package com.example.site_management_p1.interceptor;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

public class LoginInterceptor implements HandlerInterceptor {
    private RedisTemplate redisTemplate;
    public LoginInterceptor(RedisTemplate redisTemplate){
        this.redisTemplate=redisTemplate;
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String identifying=request.getHeader("identifying");
        if(identifying==null)
            return false;
        Integer uID= (Integer) redisTemplate.opsForHash().get(identifying,"uID");
        if(uID==null){
            return false;
        }
        redisTemplate.opsForHash().getOperations().expire(identifying,30,TimeUnit.MINUTES);
        return true;
    }
}
